/*
* ==========================================底层数据层==========================================
* ==========================================用户数据块==========================================
* 1.各个用户信息类，包含了用户的信息，包括用户的id，姓名，密码，权限位，角色，角色描述
* 2. 各个用户提供序列化接口和反序列化接口，用于将用户信息存储到文件中，或者从文件中读取用户信息
* 3. 各个用户提供权限校验接口，用于校验用户的权限
* 4. 各个用户提供密码修改接口，用于修改用户的密码
* 5. 权限枚举类和角色枚举
* ==========================================用户数据块==========================================
* ==========================================底层数据层==========================================
*/

#ifndef USER_H
#define USER_H

#include <iostream>
#include <string>
#include <cstdint>
#include <vector>
#include <memory>

class User;

typedef uint16_t PB_t;//未来可能会扩充权限
typedef std::shared_ptr<User> MYPTR_T;

/*==========================================<枚举>==========================================*/
enum class Role//角色枚举，管理员创建用户时使用
{
    ADMIN=1,
    USER_INFO,
    USER_STORE_IO,
    USER_STORE_CHECK
};


enum class UPBits//角色权限位枚举
{
    //管理员权限位
    USER_ADMIN_ADD = (PB_t)0x0001 << 0,
    USER_ADMIN_DELETE = (PB_t)0x0001 << 1,
    USER_ADMIN_MODIFY = (PB_t)0x0001 << 2,
    USER_ADMIN_FIND = (PB_t)0x0001 << 3,
    USER_ADMIN_SHOW = (PB_t)0x0001 << 4,
    USER_ADMIN_SAVE = (PB_t)0x0001 << 5,
    USER_ADMIN_LOG = (PB_t)0x0001 << 6,
    USER_ADMIN_ALL = (PB_t)0xFFFF,


    //录入配件人员权限位
    USER_INFO_ADD = (PB_t)0x0001 << 0,
    USER_INFO_DELETE = (PB_t)0x0001 << 1,
    USER_INFO_MODIFY = (PB_t)0x0001 << 2,
    USER_INFO_FIND = (PB_t)0x0001 << 3,
    USER_INFO_SHOW = (PB_t)0x0001 << 4,
    USER_INFO_ASSIGNING = (PB_t)0x0001 << 5,
    USER_INFO_ALL = (PB_t)0xFFFF,

    //仓库管理人员权限位
    USER_SIO_IN_ADD = (PB_t)0x0001 << 0, 
    USER_SIO_IN_DELETE = (PB_t)0x0001 << 1,
    USER_SIO_IN_MODIFY = (PB_t)0x0001 << 2,
    USER_SIO_IN_FIND = (PB_t)0x0001 << 3,
    USER_SIO_IN_SHOW = (PB_t)0x0001 << 4,
    USER_SIO_IN_ALL = (PB_t)0x001F,

    USER_SIO_OUT_ADD = (PB_t)0x0001 << 5,
    USER_SIO_OUT_DELETE = (PB_t)0x0001 << 6,
    USER_SIO_OUT_MODIFY = (PB_t)0x0001 << 7,
    USER_SIO_OUT_FIND = (PB_t)0x0001 << 8,
    USER_SIO_OUT_SHOW = (PB_t)0x0001 << 9,
    USER_SIO_OUT_ALL = (PB_t)USER_SIO_OUT_ADD|USER_SIO_OUT_DELETE|USER_SIO_OUT_MODIFY|USER_SIO_OUT_FIND|USER_SIO_OUT_SHOW,

    USER_SIO_OPERATION_IN = (PB_t)0x0001 << 10,
    USER_SIO_OPERATION_OUT = (PB_t)0x0001 << 11,
    USER_SIO_ALL = (PB_t)0xFFFF,

    //仓库盘点人员权限位
    USER_SCK_START_CHECK = (PB_t)0x0001,
    USER_SCK_SHOW_CHECK = (PB_t)0x0001 << 1,
    USER_SCK_SHOW_HISCHECK = (PB_t)0x0001 << 2,
    USER_SCK_AFFRIM = (PB_t)0x0001 << 3,
    USER_SCK_ALL = (PB_t)0xFFFF,

};

/*==========================================<用户信息基类>==========================================*/
class User
{
protected:
    int id;
    std::string name;
    std::string password;
    PB_t permissionBits;
    Role role;
    std::string roleDescription;

public:
    User() = default;
    User(const int& id, const std::string& name, const std::string& password, PB_t permissionBits, const std::string& roleDescription);
    virtual ~User();
    
    int getId()const;
    std::string getName()const;
    std::string getPassword()const;
    PB_t getPermissionBits()const;
    Role getRole()const;
    std::string getRoleDescription()const;

    void setPermissionBits(PB_t permissionBits);
    void setRole(Role role);

    virtual int check_permission(UPBits pb);
    void change_password(const std::string& newPassword);

    std::string serialize(void);
    int deSerialize(const std::string& str);
};


/*==========================================<管理员类，用户类派生>==========================================*/
class Admin : public User
{
public:
    Admin(const int& id, const std::string& name, const std::string& password, PB_t permissionBits, const std::string& roleDescription);
    Admin(const User& user);
    ~Admin();

    int add_user(std::vector<MYPTR_T>& userLib, User& user, Role role);
    int delete_user(std::vector<MYPTR_T>& userLib, int id);//根据id删除用户，后面如果想实现其他途径删除用户，重载吧
    int modify_user(std::vector<MYPTR_T>& userLib, int id, const User& user);
    MYPTR_T find_user(std::vector<MYPTR_T>& userLib, int id);
    int modify_user_permission(std::vector<MYPTR_T>& userLib, int id, PB_t permissionBits);//修改用户权限

    int check_permission(UPBits pb) override;

};

/*==========================================<配件信息人员类，用户类派生>==========================================*/
class User_info : public User
{
public:
    User_info(const int& id, const std::string& name, const std::string& password, PB_t permissionBits, const std::string& roleDescription);
    User_info(const User& user);
    ~User_info();

    int check_permission(UPBits pb) override;
};

/*==========================================<仓库管理人员，用户类派生>==========================================*/
class User_storeIO : public User
{
public:
    User_storeIO(const int& id, const std::string& name, const std::string& password, PB_t permissionBits, const std::string& roleDescription);
    User_storeIO(const User& user);
    ~User_storeIO();

    int check_permission(UPBits pb) override;
};

/*==========================================<仓库盘点人员，用户类派生>==========================================*/
class User_storeCheck : public User
{
public:
    User_storeCheck(const int& id, const std::string& name, const std::string& password, PB_t permissionBits, const std::string& roleDescription);
    User_storeCheck(const User& user);
    ~User_storeCheck();
    
    int check_permission(UPBits pb) override;

};

#endif